package com.zhanpple.color_picker.library

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

/**
 * @author zhangmingping
 * @date 2022/2/22 11:04 上午
 * des:HSVColor
 */
class ColorShowPreView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)


    var mColor: Int = Color.RED
        set(value) {
            if (field != value) {
                field = value
                postInvalidate()
            }
        }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        mPaint.color = Color.GRAY
        canvas?.drawRect(0F, 0F, measuredWidth.toFloat(), measuredHeight.toFloat(), mPaint)
        mPaint.color = Color.WHITE
        canvas?.drawRect(
            paddingLeft.toFloat(),
            paddingTop.toFloat(), measuredWidth / 2F,
            (measuredHeight - paddingBottom).toFloat(), mPaint
        )
        mPaint.color = Color.BLACK
        canvas?.drawRect(
            measuredWidth / 2F,
            paddingTop.toFloat(),
            measuredWidth.toFloat() - paddingRight,
            (measuredHeight - paddingBottom).toFloat(),
            mPaint
        )
        mPaint.color = mColor
        canvas?.drawRect(
            paddingLeft.toFloat(),
            paddingTop.toFloat(),
            measuredWidth.toFloat() - paddingRight,
            (measuredHeight - paddingBottom).toFloat(),
            mPaint
        )
    }
}